package cn.zyjblogs.utils.word;


import org.jsoup.Jsoup;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * copyright (C), 2024,北京同创永益科技发展有限公司
 *
 * @author xiaodi
 * <author>                     <time>                 <version>                   <description>
 * xiaodi                  2024/9/26 17:49:14           1.0.0                      测试
 */
public class TestClient {
    public static void main(String[] args) throws IOException {
        String directoryPath = "G:\\2024gitee\\word-handler\\src\\main\\resources";
        try (Stream<Path> paths = Files.walk(Paths.get(directoryPath))) {
            List<Path> pathList = paths.filter(Files::isRegularFile)
                    .filter(p -> p.toString().endsWith(".html")).collect(Collectors.toList());
            int size = pathList.size();
            // size  = 3  i = 0 1 2
            for (int i = 0; i < size; i++) {
                Path path = pathList.get(i);
                String htmlContent = Jsoup.parse(Files.readString(path, StandardCharsets.UTF_8)).body().html();
                // 如果不是最后一次，则添加{{content}}占位符
                if (i + 2 <= size) {
                    htmlContent = htmlContent + "{{content}}";
                }
                // 如果是第一次则读取模板
                if (i == 0) {
                    HtmlToWordConverter.converterToFileByTemplate("demo.docx", htmlContent);
                } else {
                    HtmlToWordConverter.converterToFileByTemplatePath("demo.docx", htmlContent, "G:\\2024gitee\\word-handler\\demo.docx");
                }
                Thread.sleep(2 * 1000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


//        // 读取resource下的test3.html,防止打成jar无法读取
//        InputStream resourceAsStream = TestClient.class.getResourceAsStream("/test3.html");
//        //  转换成string
//        String s = new String(resourceAsStream.readAllBytes(), StandardCharsets.UTF_8);
//
//        byte[] bytes = Files.readAllBytes(Path.of("G:\\2024gitee\\word-handler\\src\\main\\resources\\test3.html"));
//        String s = new String(bytes, StandardCharsets.UTF_8);
//
//        HtmlToWordConverter.converterToFileByTemplate("demo.docx", s);
//        HtmlToWordConverter.converterToFileByTemplatePath("demo.docx", s, "G:\\2024gitee\\word-handler\\demo.docx");
//
//
//        // path:file:///G:/2024gitee/word-handler/target/word-handler-2.3.4.RELEASE-jar-with-dependencies.jar
//        // 获取jar包所在路径
//        String path = TestClient.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//        // 输出jar包所在路径，区分linux和windows
//        String os = System.getProperty("os.name").toLowerCase();
//        if (os.contains("windows")) {
//            path = path.substring(1).replace("/", "\\");
//        }
//        System.out.println("path:" + path);
//
//
//        // 获取jar所在路径，根据系统类型使用对应的路径分隔符
//        // path:/hatech/app/worker/word-handler-2.3.4.RELEASE-jar-with-dependencies.jar
//        // jarPath:/hatech/app/worker
//        String separator = System.getProperty("file.separator");
//        String jarPath = path.substring(0, path.lastIndexOf(separator));
//        System.out.println("jarPath:" + jarPath);


    }
}
